package com.bskj.kuaifu.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.bskj.common.bean.model.ParkInfo;
import com.bskj.common.constants.ReturnCodeMsgConstants;
import com.bskj.common.dao.core.FindRequestEntity;
import com.bskj.common.dao.core.WhereEnum;
import com.bskj.common.enums.ParkChargeTypeEnum;
import com.bskj.common.utils.LogUtils;
import com.bskj.common.utils.NetworkUtil;
import com.bskj.common.web.BaseController;
import com.bskj.kuaifu.bean.vo.req.ConfirmReceiveMoneyReqVo;
import com.bskj.kuaifu.bean.vo.req.GetCarNoInfoReqVo;
import com.bskj.kuaifu.bean.vo.req.OfflineDataBackupReqVo;
import com.bskj.kuaifu.bean.vo.req.ParkReqVo;
import com.bskj.kuaifu.service.IParkInfoService;

@Controller
@RequestMapping("parkInfo")
public class ParkInfoController extends BaseController {
	@Autowired
	private IParkInfoService parkInfoService;

	/**
	 * 车辆出停车场需要收费时调用该接口,----欠费用户出停车场需要交钱的时候，发送微信模版消息
	 *
	 * @param reqVo
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("park")
	public ModelMap park(ParkReqVo reqVo,HttpServletRequest request) throws Exception {
		LogUtils.debug(JSONObject.toJSONString(getRequest().getParameterMap()));
		ModelMap modeMap = new ModelMap();
		/* 
		 * 1、调用微信无感支付平台接口
		 * 2、如果返回值是   是支持无感支付后付费的，且支付成功的，流程走完，返回开闸信息
		 * 3、如果返回值是   是用户不支持无感支付后付费的，push一条消息，打开这条信息，有支付界面，让用户完成支付
		 */
		String ip = NetworkUtil.getIpAddress(request);
		Map<String, Object> resultMap = parkInfoService.carOut(reqVo, ip);
		modeMap.putAll(resultMap);
		return modeMap;
	}

	/**
	 * 停车场确认收费时 调用该接口（和用户确认平台支付或者现金支付以后）
	 *
	 * @param reqVo
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("confirmReceiveMoney")
	public ModelMap confirmReceiveMoney(ConfirmReceiveMoneyReqVo reqVo, HttpServletRequest request) throws Exception {
		LogUtils.debug(JSONObject.toJSONString(getRequest().getParameterMap()));
		ModelMap modeMap = new ModelMap();
		String ip = NetworkUtil.getIpAddress(request);
		Map<String, Object> resultMap = parkInfoService.confirmReceiveMoney(reqVo, ip);
		modeMap.putAll(resultMap);
		return modeMap;
	}

	
	/**
	 * 备份离线数据
	 * 
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("offlineDataBackup")
	public Map<String, Object> backupOfflineData(OfflineDataBackupReqVo reqVo) throws Exception {
		parkInfoService.checkOfflineData(reqVo);
		ModelMap modeMap = new ModelMap();
		modeMap.put("resultCode", ReturnCodeMsgConstants.SUCCESS_CODE);
		modeMap.put("resultMsg", "备份成功");
		return modeMap;
	}

	/**
	 * 车辆进场，调用无感支付，如果已经绑定无感，拉取用户信息
	 *  
	 * @param reqVo
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("getCarNoInfo")
	public ModelMap getCarNoInfo(GetCarNoInfoReqVo reqVo) throws Exception {
		LogUtils.debug(JSONObject.toJSONString(getRequest().getParameterMap()));
		ModelMap modeMap = new ModelMap();
		parkInfoService.carIn(reqVo);
		modeMap.put("resultCode", ReturnCodeMsgConstants.SUCCESS_CODE);
		return modeMap;
	}
	
	
	/**
	 * 校验数据
	 * @param reqVo
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("checkPark")
	public ModelMap checkPark(ParkReqVo reqVo) throws Exception {
		ModelMap modeMap = new ModelMap();
		
		FindRequestEntity reqEntity = new FindRequestEntity();
		reqEntity.addWhere("car_no", WhereEnum.EQ, reqVo.getCarNo());
		reqEntity.addWhere("charge", WhereEnum.EQ, reqVo.getCharge());
		reqEntity.addWhere("in_time", WhereEnum.EQ, reqVo.getInTime());
		reqEntity.addWhere("out_time", WhereEnum.EQ, reqVo.getOutTime());
		reqEntity.addWhere("park_lot_custimer_id", WhereEnum.EQ, reqVo.getParkLotCustomerId());
		reqEntity.addWhere("box_no", WhereEnum.EQ, reqVo.getBoxNo());
		List<ParkInfo> parkInfoList = parkInfoService.find(reqEntity);
		ParkInfo parkInfo = null;
		if(parkInfoList != null && parkInfoList.size() > 0) {
			parkInfo = parkInfoList.get(0);
		}
		
		if(parkInfo != null && ParkChargeTypeEnum.PLATFROM_PAY.getKey() == parkInfo.getChargeType())  {
			modeMap.put("resultCode", ReturnCodeMsgConstants.SYS_PAY_CODE);
			modeMap.put("resultMsg", ReturnCodeMsgConstants.SYS_PAY_MSG);
			return modeMap;
		}
		
		modeMap.put("resultCode", ReturnCodeMsgConstants.SUCCESS_CODE);
		return modeMap;
	}
}
